package com.njh.tracker.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.njh.tracker.R;
import com.njh.tracker.base.BaseRecyclerAdapter;
import com.njh.tracker.base.CommonHolder;
import com.njh.tracker.bean.SettingInfoBean;
import com.njh.tracker.comm.SP;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by niejiahuan on 17/4/25.
 */

public class LoseSetAdpater extends BaseRecyclerAdapter<SettingInfoBean> {
    @Override
    public CommonHolder setViewHolder(ViewGroup parent) {
        return new LoseHodler(parent.getContext(),parent, R.layout.item_lose);
    }
    class LoseHodler extends CommonHolder<SettingInfoBean>{
        TextView tvLose;
        TextView tvTime;
        Button btnDel;
        Button btnLoction;
        public LoseHodler(Context context, ViewGroup root, int layoutRes) {
            super(context, root, layoutRes);
        }

        @Override
        public void bindData(final SettingInfoBean settingInfoBean) {
            tvLose= (TextView) itemView.findViewById(R.id.tv_title);
            btnDel= (Button) itemView.findViewById(R.id.btn_del);
            tvTime=(TextView) itemView.findViewById(R.id.tv_time);
            setTextValue(tvLose,settingInfoBean.getName());
            setTextValue(tvTime,"丢失时间:"+settingInfoBean.getUpdatedAt());
            btnDel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    settingInfoBean.delete(new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            if (null==e){
                                BmobQuery<SettingInfoBean> bmobQuery = new BmobQuery<SettingInfoBean>();
                                bmobQuery.addWhereEqualTo("phone", SP.get(getContext(), "phone",""));
                                bmobQuery.findObjects(new FindListener<SettingInfoBean>() {
                                    @Override
                                    public void done(List<SettingInfoBean> list, BmobException e) {
                                        if (null==e){
                                            setDataList(list);
                                        }
                                    }
                                });
                            }
                        }
                    });
                }
            });
            btnLoction= (Button) itemView.findViewById(R.id.btn_loction);
            btnLoction.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Uri uri = Uri.parse("geo:"+settingInfoBean.getLatitude()+","+settingInfoBean.getLongitude());
                    Intent it = new Intent(Intent.ACTION_VIEW,uri);
                    getContext().startActivity(it);
                }
            });
        }

    }
}
